//
//  DropDownMenu.m
//  OAProject
//
//  Created by yhj on 2017/3/9.
//  Copyright © 2017年 cdnunion. All rights reserved.
//

#import "DropDownMenu.h"

static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";
@interface DropDownMenu ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIImageView *arrowIcon;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray *titles;

@property(nonatomic,assign)CGFloat width;

@property(nonatomic,strong)UIWindow *window;

//@property(nonatomic,assign)BOOL isMenu;

@property(nonatomic,assign)NSInteger selectedIndex;

@end


#define kDuration 0.3

@implementation DropDownMenu

-(instancetype)initWithWidth:(CGFloat)width titles:(NSArray *)titles
{
    if (self=[super init])
    {
        _width=width;
        _titles=titles;

        _window=[UIApplication sharedApplication].keyWindow;
        self.frame=CGRectMake(0,0,100,30);

        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickSelf:)];
        [self addGestureRecognizer:tap];

        self.titleLabel.font=Font_Number(20);

        self.arrowIcon.hidden=NO;

        _isMenu=NO;

        _selectedIndex=-1;

        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
    return self;
}

-(void)clickSelf:(UITapGestureRecognizer *)tap
{
    if (!_isMenu)
    {
        [UIView animateWithDuration:kDuration animations:^{
            self.arrowIcon.transform=CGAffineTransformRotate(self.arrowIcon.transform,M_PI);
            self.tableView.frame=CGRectMake((APPW-_width)/2,64,_width,_titles.count*50);
        }];
        [self.tableView reloadData];
        _isMenu=YES;
    }
    else
    {
        [self dismissControl];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titles.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.text=_titles[indexPath.row];
    cell.textLabel.textColor=BlackColor;
    cell.textLabel.font=Font_Number(18);
    cell.backgroundColor=WhiteColor;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [self dismissControl];

    self.selectedAtIndex(indexPath.row);

    self.titleLabel.text=_titles[indexPath.row];

    _selectedIndex=indexPath.row;
}


-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel=[UILabel new];
        _titleLabel.textColor=WhiteColor;
        _titleLabel.text= _titles[_selectedIndex];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
    }
    return _titleLabel;
}


-(UIImageView *)arrowIcon
{
    if (!_arrowIcon) {
        _arrowIcon=[UIImageView new];
        _arrowIcon.image=[UIImage imageNamed:@"task_next"];
        [self addSubview:_arrowIcon];
        [_arrowIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(self.titleLabel.mas_right).mas_equalTo(5);
        }];
    }
    return _arrowIcon;
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc] initWithFrame:CGRectMake((APPW-_width)/2,64,_width,0) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        ViewBorderRadius(_tableView,5,2,BGColor);

        _tableView.tableFooterView=[UIView new];
        _tableView.scrollEnabled=NO;
        _tableView.backgroundColor=WhiteColor;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
        [_window addSubview:_tableView];
    }
    return _tableView;
}


- (void)dismissControl
{
    [UIView animateWithDuration:kDuration animations:^{
        self.arrowIcon.transform=CGAffineTransformRotate(self.arrowIcon.transform,M_PI);
        self.tableView.frame=CGRectMake((APPW-_width)/2,64,_width,0);
    }];
    _isMenu=NO;
}

-(void)selectedAtIndex:(SelectedAtIndex)block
{
    self.selectedAtIndex=block;
}

// 间隙置边
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
